/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.QuestionDAO;
import dto.Answer;
import dto.Question;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.SharingUtil;

/**
 *
 * @author PhucAn
 */
public class ModifyQuestionAction extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      String deleteAction=request.getParameter("delete");
      String updateAction=request.getParameter("update");
      QuestionDAO daoQuesion=new QuestionDAO();
      int id=Integer.parseInt(request.getParameter("id"));
      if(!deleteAction.endsWith("")){
          if(daoQuesion.delete(id)>0){
              response.sendRedirect("/admin/index?pcontent=manage-question");
          }else {
              SharingUtil.showMessage(request, "Error occured. Question cannot be deleted");
          }
      }else if(!updateAction.equals("")){
          Question q=new Question();
          String[] answers=request.getParameterValues("answer");
          String singleAns=request.getParameter("single-ans-result");
          String[] multiAns=request.getParameterValues("multi-ans-result");
          boolean isSingle=(singleAns!=null);
          String questionContent=request.getParameter("content");
          
          q.setContent(request.getParameter("content"));
          q.getCategory().setId(Integer.parseInt(request.getParameter("category-id")));
          for(String st:answers){
              Answer ans=new Answer();
              ans.setContent(st);
              q.getAnswers().add(ans);
          }
          q.setLevel(Byte.parseByte(request.getParameter("level")));
          q.setMark(Float.parseFloat(request.getParameter("mark")));
          if(isSingle){
              q.getAnswers().get(Integer.parseInt(singleAns)).setIsTrue(true);
          }else{
              for(String st:multiAns){
                  int idx=Integer.parseInt(st);
                  q.getAnswers().get(idx).setIsTrue(true);
              }
          }
          if(daoQuesion.update(q)>0){
              SharingUtil.showMessage(request, "Your change is saved");
          }else{
              SharingUtil.showMessage(request, "Error occured. Action failed");
          }
          
      }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
